﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using FantastPlatform.Framework.Model;
using FantastPlatform.Decision.IService;
using FantastPlatform.Decision.IModel;
using FantastPlatform.Decision.Model;
using FantastPlatform.Framework.IService;
using FantastPlatform.Framework.IModel;

namespace FantastPlatform.Decision.UI.Service
{
    public partial class FlowFileViewService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoginEmployee = Session["User"] as FantastPlatform.Framework.Model.Employee;
            IFlowFileService flowFileService = ServiceFactory.CreateService<IFlowFileService>();
            ConditionGroup group = new ConditionGroup();
            Condition enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
            group.Conditions.Add(enableCondition);
            int totalCount = 0;
            switch (Action)
            {
                case "Add":
                    string data = Data;
                    IFlowFile flowFile = (IFlowFile)JsonToEntity<FlowFile>(RepairString(data));
                    // GetLeaders(flowFile);
                    if (flowFile.Id == 0)
                    {
                        flowFile.Enable = true;
                        flowFile.CreateDate = DateTime.Now;
                        flowFileService.Create(flowFile);
                        ReturnSuccessMessage("保存应急预案成功");
                    }
                    else
                    {
                        IFlowFile OldMaintainStability = flowFileService.Get(flowFile.Id);
                        OldMaintainStability.ModifyDate = DateTime.Now;
                        OldMaintainStability.Name = flowFile.Name;
                        OldMaintainStability.EchelonNumber = flowFile.EchelonNumber;
                        OldMaintainStability.Leader = flowFile.Leader;
                        OldMaintainStability.LevelNumber = flowFile.LevelNumber;
                        OldMaintainStability.Range = flowFile.Range;
                        OldMaintainStability.Require = flowFile.Require;
                        flowFileService.Update(OldMaintainStability);
                        ReturnSuccessMessage("更新应急预案成功");
                    }
                    break;
                case "AddFlowImplement":
                    int flowId = 0;
                    if (!string.IsNullOrEmpty(Request["flowId"]))
                    {
                        flowId = Int32.Parse(Request["flowId"]);
                    }
                    if (!string.IsNullOrEmpty(Request["level"]))
                    {
                        string level = Request["level"];
                    }
                    IFlowImplementService implementServcie = ServiceFactory.CreateService<IFlowImplementService>();
                    IFlowImplement flowImplement = (IFlowImplement)JsonToEntity<FlowImplement>(RepairString(Data));
                    // GetLeaders(flowElement);
                    if (flowImplement.Id == 0)
                    {
                        ReturnSuccessMessage("更改预案信息失败");
                    }
                    else
                    {
                        IFlowImplement OldItem = implementServcie.Get(flowImplement.Id);
                        OldItem.ModifyDate = DateTime.Now;
                        OldItem.Name = flowImplement.Name;
                        OldItem.Status = flowImplement.Status;
                        OldItem.Leader = flowImplement.Leader;
                        OldItem.EndTime = flowImplement.EndTime;

                        OldItem.Require = flowImplement.Require;
                        OldItem.TotalPerson = flowImplement.TotalPerson;
                        implementServcie.Update(OldItem);
                        ReturnSuccessMessage("更新预案信息成功");
                    }
                    break;
                case "Search":
                    IList<Sort> sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    IList<IFlowFile> flowFileList = flowFileService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    var obj = from n in flowFileList
                              select new
                              {
                                  Id = n.Id,
                                  Name = n.Name,
                                  EchelonNumber = n.EchelonNumber,
                                  Leader = n.Leader,
                                  LevelNumber = n.LevelNumber,
                                  Range = n.Range,
                                  Require = n.Require
                              };
                    ReturnList(totalCount, obj.ToList());
                    break;
                case "SearchByCondition":
                    string Searchjson = RepairString(Data);
                    flowFile = (IFlowFile)JsonToEntity<MaintainStability>(Searchjson);
                    enableCondition = new Condition() { Type = typeof(bool).FullName, Field = "Enable", Value1 = true };
                    Condition condition1 = new Condition() { Type = typeof(string).FullName, Field = "Name", Value1 = '%' + flowFile.Name + '%', Comparison = Comparisons.Like, Relation = Relations.And };
                    group.Conditions.Add(enableCondition);
                    group.Conditions.Add(condition1);
                    sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Asc });
                    flowFileList = flowFileService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    obj = from n in flowFileList
                          select new
                          {
                              Id = n.Id,
                              Name = n.Name,
                              EchelonNumber = n.EchelonNumber,
                              Leader = n.Leader,
                              LevelNumber = n.LevelNumber,
                              Range = n.Range,
                              Require = n.Require
                          };
                    var objList = obj.ToList();
                    ReturnList(objList.Count, objList);
                    break;
                case "Get":
                    flowFile = flowFileService.Get(Id);
                    object objFlowFile = (new
                                     {
                                         Id = flowFile.Id,
                                         Name = flowFile.Name,
                                         EchelonNumber = flowFile.EchelonNumber,
                                         Leader = flowFile.Leader,
                                         LevelNumber = flowFile.LevelNumber,
                                         Range = flowFile.Range,
                                         Require = flowFile.Require
                                     });
                    ReturnObj(objFlowFile);
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的预案ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    flowFileService.Delete(idList);
                    ReturnSuccessMessage("删除预案成功");
                    break;
                case "SearchImplement":
                    sorts = new List<Sort>();
                    sorts.Add(new Sort() { Field = "CreateDate", Order = Orders.Desc });
                    IFlowImplementService flowImplementService = ServiceFactory.CreateService<IFlowImplementService>();
                    IList<IFlowImplement> flowImplementList = flowImplementService.Search(group, sorts, PageSize, SelfPage, out totalCount);
                    var obj2 = from n in flowImplementList
                               select new
                               {
                                   Id = n.Id,
                                   Name = n.FlowLevel.FlowFile.Name,
                                   EchelonNumber = n.EchelonNumber,
                                   Leader = n.Leader,
                                   Level = GetLeveName(n.FlowLevel.Level),
                                   PersonNumber = n.TotalPerson,
                                   LevelId = n.FlowLevel.Id,
                                   LeaderId = n.Leader,
                                   Status = n.Status,
                                   Range = n.Range,
                                   Require = n.Require,
                                   StartTime = n.StartTime.ToString(),
                                   EndTime = n.EndTime.ToString()
                               };
                    var objList2 = obj2.ToList();
                    ReturnList(objList2.Count, objList2);
                    break;
                case "LoadFlowImplement":

                    flowImplementService = ServiceFactory.CreateService<IFlowImplementService>();
                    IFlowLevelService flowLevelService = ServiceFactory.CreateService<IFlowLevelService>();
                    IFlowLevel flowLevel = flowLevelService.Get(Id);
                    flowImplement = flowImplementService.Implement(flowLevel);
                    if (flowImplement != null)
                    {
                        object flowImplementObj = new { Id = flowImplement.Id };
                        ReturnObj(flowImplementObj);
                    }
                    else
                    {
                        ReturnErrorMessage("预案启动失败");
                    }
                    break;
                case "GetImplement":
                    IFlowImplementService flowImplementServce = ServiceFactory.CreateService<IFlowImplementService>();
                    flowImplement = flowImplementServce.Get(Id);
                    object objFlowImplement = (new
                                     {
                                         Id = flowImplement.Id,
                                         Name = flowImplement.FlowLevel.FlowFile.Name,
                                         Status = flowImplement.Status,
                                         TotalPerson = flowImplement.TotalPerson,
                                         Leader = flowImplement.Leader,
                                         Level = GetLeveName(flowImplement.FlowLevel.Level),
                                         Range = flowImplement.Range,
                                         Require = flowImplement.FlowLevel.FlowFile.Require,
                                         StartTime = flowImplement.StartTime,
                                         EndTime = flowImplement.EndTime
                                     });
                    ReturnObj2(objFlowImplement);
                    break;

            }
        }
        private string GetLeveName(string p)
        {
            string levelName = "";
            switch (p)
            {
                case "1": levelName = "A级加强";
                    break;
                case "2": levelName = "A级";
                    break;
                case "3": levelName = "B级";
                    break;
                case "4": levelName = "C级";
                    break;
                case "5": levelName = "D级";
                    break;
            }
            return levelName;
        }
    }
}